package com.bawei.db.database

import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.DeleteColumn
import androidx.room.DeleteTable
import androidx.room.RenameColumn
import androidx.room.RenameTable
import androidx.room.RoomDatabase
import androidx.room.migration.AutoMigrationSpec
import com.bawei.db.dao.UserInfoDao
import com.bawei.db.entity.UserInfoEntity

/**
 * Author: Jason Zhang
 * Date: 2024/12/13
 * Desc: 数据库实体对象
 */
@Database(
    entities = [UserInfoEntity::class], //数据库中包含实体对象
    version = 1, //数据库版本
    exportSchema = false, //是否支持导出数据库
    /**
     * 数据升级策略：一定对旧的数据库中表升级，包含表名，字段，尽量不要修改字段名或者删除，一般更多关注新增字段
     */
    autoMigrations = [
//        AutoMigration(from = 1, to= 2, spec = UserInfoTableNameAutoMigration::class),
//        AutoMigration(from = 1, to= 2, spec = UserInfoColumnAutoMigration::class),
//        AutoMigration(from = 1, to= 2, spec = DeleteUserInfoAutoMigration::class),
//        AutoMigration(from = 1, to= 2, spec = DeleteUserInfoColumnAutoMigration::class),
    ]
)

abstract class AppDataBase : RoomDatabase() {
    //获取操作Dao对象
    abstract fun getUserInfoDao(): UserInfoDao
}

/**
 * 数据库升级：修改表名
 */
@RenameTable(fromTableName = "", toTableName = "")
class UserInfoTableNameAutoMigration : AutoMigrationSpec

/**
 * 数据库升级：修改表字段
 */
@RenameColumn(tableName = "user_table", fromColumnName = "name", toColumnName = "user_name")
class UserInfoColumnAutoMigration : AutoMigrationSpec

/**
 * 数据库升级：删除表格
 */
@DeleteTable(tableName = "user_table")
class DeleteUserInfoAutoMigration : AutoMigrationSpec

/**
 * 数据库升级：删除字段
 */
@DeleteColumn(tableName = "user_table", columnName = "")
class DeleteUserInfoColumnAutoMigration : AutoMigrationSpec



